package com.iclub.service.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iclub.dao.MessageDao;
import com.iclub.model.Message;
import com.iclub.service.MessageService;
import com.iclub.util.Constants;

@Service
public class MessageServiceImpl extends BaseService  implements MessageService{
	
	@Override
	public Map<String, Object> insertMessage(Message msg)  {
		Map<String,Object> res=new HashMap<String, Object>();
		try {
			messageDao.insertMessage(msg);
			res.put(Constants.KEY_RESCODE, Constants.SUCCESS_RESCODE);
			res.put(Constants.KEY_RESMSG, Constants.SUCCESS_RESMSG);
		} catch (SQLException e) {
			log_detail.error("消息入库,系统异常",e);
			res.put(Constants.KEY_RESCODE, Constants.FAIL_RESCODE);
			res.put(Constants.KEY_RESMSG, Constants.FAIL_RESMSG);
		}
		return res;
	}

	@Override
	public Map<String, Object> updateMessageState(int msgId){
		Map<String,Object> res=new HashMap<String, Object>();
		try {
			messageDao.updateMessageState(msgId);
			res.put(Constants.KEY_RESCODE, Constants.SUCCESS_RESCODE);
			res.put(Constants.KEY_RESMSG, Constants.SUCCESS_RESMSG);
		} catch (SQLException e) {
			log_detail.error("用户消息状态,系统异常",e);
			res.put(Constants.KEY_RESCODE, Constants.FAIL_RESCODE);
			res.put(Constants.KEY_RESMSG, Constants.FAIL_RESMSG);
		}
		return res;
	}

	@Override
	public Map<String, Object> selectMessageList(Map<String, String> map)
			 {
		Map<String,Object> res=new HashMap<String, Object>();
		List<Message> msglist=null;
		try {
			msglist=messageDao.selectMessageList(map);
			if(msglist!=null&&msglist.size()>0){
				msglist=dealWithTimeStamp(msglist);
				res.put(Constants.KEY_RESCODE, Constants.SUCCESS_RESCODE);
				res.put(Constants.KEY_RESMSG, Constants.SUCCESS_RESMSG);
				res.put(Constants.KEY_RESCONTENT, msglist);
			}
			else{
				res.put(Constants.KEY_RESCODE, Constants.SUCCESS_RESCODE);
				res.put(Constants.KEY_RESMSG, Constants.NORESULT_RESMSG);
			}
		} catch (SQLException e) {
			log_detail.error("查询消息列表,系统异常",e);
			res.put(Constants.KEY_RESCODE, Constants.FAIL_RESCODE);
			res.put(Constants.KEY_RESMSG, Constants.FAIL_RESMSG);
		}
		return res;
	}
	
	public List<Message> dealWithTimeStamp(List<Message> msglist){

		for(Message msg:msglist){
			msg.setSendTime(msg.getSendTime().replace(".0", ""));
		}
		return msglist;
	}
	

}
